搜索 K
Appearance
博客正在加载中...
Appearance
接下来讲讲 Junit
Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库。
JUnit5 与之前版本的 Junit 框架有很大的不同,由三个不同子项目的几个不同模块组成:
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
说明:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> 分析依赖可知,确实用的是 Junit5:

在代码中,引入的也是 jupiter:
需要注意的是,在 SpringBoot 2.4 版本之后,移除了 Vintage,想要兼容得自行添加依赖,参考 发行说明:
JUnit 5’s Vintage Engine Removed from
spring-boot-starter-testIf you upgrade to Spring Boot 2.4 and see test compilation errors for JUnit classes such as
org.junit.Test, this may be because JUnit 5’s vintage engine has been removed fromspring-boot-starter-test. The vintage engine allows tests written with JUnit 4 to be run by JUnit 5. If you do not want to migrate your tests to JUnit 5 and wish to continue using JUnit 4, add a dependency on the Vintage Engine, as shown in the following example for Maven:xml<dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> </exclusion> </exclusions> </dependency>1
2
3
4
5
6
7
8
9
10
11
SpringBoot 整合 Junit 后,怎么用:
JUnit5 的注解与 JUnit4 的注解有所变化,更多的可以参考 官网文档,大致如下
@DisplayName 我们新增一个测试类:
package com.peterjxl.learnspringbootwebadmin;
import org.junit.jupiter.api.Test;
public class Junit5Test {
@Test
void testDisplayName(){
System.out.println("testDisplayName");
}
} 运行结果:默认左侧展示的是类名和方法名:

如果加上了 @DisplayName:
package com.peterjxl.learnspringbootwebadmin;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Junit5功能测试类")
public class Junit5Test {
@Test
@DisplayName("测试DisplayName")
void testDisplayName(){
System.out.println("testDisplayName");
}
} 运行结果:

@BeforeEach,@AfterEach 新增方法:
@BeforeEach
void testBeforeEach(){
System.out.println("测试就要开始了.....");
}
@AfterEach
void testAfterEach(){
System.out.println("测试结束了.....");
} 运行结果:
测试就要开始了.....
testDisplayName
测试结束了.....@BeforeEach 是会在 每一个 测试方法之前都执行,注意是每一个。我们可以新增多一个测试方法:
@Test
@DisplayName("测试方法2")
void test2(){
System.out.println("test2");
}然后我们直接运行整个测试类,这样每个测试方法都会执行一遍:

运行结果:
测试就要开始了.....
test2
测试结束了.....
测试就要开始了.....
testDisplayName
测试结束了.....
@BeforeAll
static void testBeforeAll(){
System.out.println("所有测试就要开始了.....");
}
@AfterAll
static void testAfterAll(){
System.out.println("所有测试结束了.....");
}运行结果:
所有测试就要开始了.....
测试就要开始了.....
test2
测试结束了.....
测试就要开始了.....
testDisplayName
测试结束了.....
所有测试结束了..... 注意 @BeforeAll 和 @AfterAll 的方法必须加上 static。
@Disabled @Disabled 的方法相当于不执行:
@Test
@DisplayName("测试方法2")
@Disabled
void test2(){
System.out.println("test2");
}新增方法:
@Test
@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
void testTimeout() throws InterruptedException {
Thread.sleep(600);
}这里我们指定数值是 500,单位是毫秒,也就是 500 毫秒后超时(也可以不指定单位,默认是秒) 运行结果:
所有测试就要开始了.....
测试就要开始了.....
测试结束了.....
java.util.concurrent.TimeoutException: testTimeout() timed out after 500 milliseconds
... 58 more在使用 Spring 时,我们 Spring 整合 Junit 是要用注解 @RunWith 的,不然就不能使用 @Autowired;
在 SpringBoot 中,我们直接使用 @SpringbootTest,即可使用 SpringBoot 的功能,而该注解其实是一个复合注解,使用了 @ExtendWith,@ExtendWith 就相当于 @RunWith:
@ExtendWith({SpringExtension.class})
public @interface SpringBootTest 如果我们想要使用其他平台,就得修改 @ExtendWith 里的值,这就是该注解的作用
新增方法:
@RpeatedTest(5)
void test5(){
System.out.println("test5");
} 运行结果:
